package frobagroup.services.backstage.impl;

import com.google.common.collect.ImmutableMap;
import frobagroup.entities.backstage.Feedback;
import frobagroup.entities.backstage.StaffProfile;
import frobagroup.entities.frontEnd.UserProfile;
import frobagroup.pagination.PageableImpl;
import frobagroup.pagination.Pagination;
import frobagroup.repositories.backstage.FeedbackRepository;
import frobagroup.repositories.backstage.StaffProfileRepository;
import frobagroup.repositories.frontEnd.UserProfileRepository;
import frobagroup.services.backstage.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * Created by Tinsley on 2018/6/19.
 */
@Service
public class FeedbackServiceImpl implements FeedbackService{

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private FeedbackRepository feedbackRepository;

    @Autowired
    private StaffProfileRepository staffProfileRepository;

    @Override
    public Pagination findFeedbacks(Pagination pagination, String uId) {
        String sort = Objects.isNull(pagination.getSort()) ? "commitTime" : pagination.getSort();
        PageableImpl pageable = new PageableImpl(pagination.getPageNumber(),pagination.getPageSize(),
                new Sort(Objects.equals(Sort.Direction.DESC.name(),pagination.getDir()) ? Sort.Direction.DESC:Sort.Direction.ASC,sort));
        Page<Feedback> pages ;

        if (Objects.equals(pagination.getConditions().get("beforeOfAfter"),"before"))
            pages = feedbackRepository.findByFbUId(uId, pageable);
        else
            pages = feedbackRepository.findAll(pageable);

        pagination.setContent(pages.getContent());
        pagination.setTotal(pages.getTotalElements());
        return pagination;
    }

    @Override
    public Feedback addFeedback(Feedback feedback, String uId) {

        feedback.setCommitTime(new Date());
        feedback.setFbUId(uId);
        return feedbackRepository.save(feedback);

    }

    @Override
    public Feedback replyFeedback(Feedback feedback, String staffId) {

        feedback.setReplyTime(new Date());
        feedback.setReplyUId(staffId);
        return feedbackRepository.save(feedback);

    }

    @Override
    public Map feedbackDetail(String fbId) {

        Feedback feedback =  feedbackRepository.findOne(fbId);
        UserProfile userProfile = userProfileRepository.findByUId(feedback.getFbUId());
        StaffProfile staffProfile = new StaffProfile();
        if (!StringUtils.isEmpty(feedback.getReplyUId()))
            staffProfile = staffProfileRepository.findBySId(feedback.getReplyUId());
        return ImmutableMap.of("feedback",feedback,"userProfile",userProfile,"staffProfile",staffProfile);
    }

}
